let { api } = require("./../../utils/api.js");
let { getTime, randomVal } = require("./../../utils/constant.js");
let placeArr = [
  "这个小程序真是 . . .",
  "请输入您对本应用的意见 , 我们将持续优化!",
  "有什么使用问题都可以跟我们说",
  "对本应用有什么看法，放开了说 . . ."
];

Page({
  data: {
    placeholder: randomVal(placeArr) //输入框的提示文案
  },
  evaluates: function() {
    wx.navigateTo({
      url: "../evaluates/evaluates"
    });
  },
  checkData: function(content) {
    if (!content) {
      wx.showToast({
        title: "请输入您宝贵的意见",
        icon: "none",
        duration: 2000
      });
      return false;
    } else {
      return true;
    }
  },
  bindFormSubmit: function(e) {
    let that = this;
    let { content, contact } = e.detail.value;
    content = content.trim();
    this.setData({
      content,
      contact
    });
    if (this.checkData(content)) {
      let obj = {
        userId: wx.getStorageSync("userInfo").userId, //意见反馈人
        content: encodeURIComponent(content), //反馈内容
        contact, //联系方式
        date: getTime(1) //反馈日期
      };
      console.log(obj);
      wx.request({
        url: api + "addFeedback.do",
        method: "POST",
        data: obj,
        success: function(res) {
          console.log(res.data);
          if (res.data.status === 1) {
            wx.showToast({
              title: "意见反馈成功！",
              icon: "success",
              duration: 2000
            });
            that.setData({
              content: "",
              contact: ""
            });
          } else {
            wx.showToast({
              title: "我在开小差 , 请稍后再试！",
              icon: "none",
              duration: 2000
            });
          }
        },
        fail: function() {
          wx.showToast({
            title: "网络错误 , 请稍后再试！",
            icon: "none",
            duration: 2000
          });
        }
      });
    }
  },
  goPhone: function() {
    wx.makePhoneCall({
      phoneNumber: "16605817970"
    });
  }
});
